package org.libjpopup.receiver;

import org.libjpopup.Popup;

import java.util.HashMap;

public class PopupParser {
    private long timeout = 40000;
    private String localhost;
    private String localIP;
    private PopupReceiverIF receiver;

    private HashMap<Integer, Long> hm = new HashMap<Integer, Long>();
    private HashMap<String, String[]> NameDataTest = new HashMap<String, String[]>();
    private static final String BROADCAST_MAC = "FFFFFFFFFFFF";


    public PopupParser(PopupReceiverIF receiver) {
        this.localhost = Receiver.getMyHostname();
        this.localIP = Receiver.getMyIp();
        this.receiver = receiver;
    }


    private synchronized void addFullPopup(String srcName, String srcIp, String srcMac, String srcNetbios, String dstName, String dstIp, String dstMac, String text) {
        Popup popup = new Popup(srcName, srcNetbios, srcIp, srcMac, dstName, dstIp, dstMac, text);
        int hash;
        if (text.length() < 1000)
            hash = (srcName + srcMac + srcIp + dstIp + dstMac + text).hashCode();
        else hash = (srcName + srcMac + srcIp + dstIp + dstMac + text.substring(500, 999)).hashCode();
        if (hm.get(Integer.valueOf(hash)) == null) {
            if (dstIp.equalsIgnoreCase(localIP)) {
                if (dstName.equalsIgnoreCase(localhost)) {
                    receiver.popupReceived(PopupReceiverIF.popupTypes.PRIVATE_IN, popup);
                } else {
                    receiver.popupReceived(PopupReceiverIF.popupTypes.PRIVATE_OTHER, popup);
                }
            } else if ((srcIp.equalsIgnoreCase(localIP)) && !dstMac.equalsIgnoreCase(BROADCAST_MAC) && srcName.equalsIgnoreCase(localhost))
                receiver.popupReceived(PopupReceiverIF.popupTypes.PRIVATE_OUT, popup);
            else if (dstMac.equalsIgnoreCase(BROADCAST_MAC))
                receiver.popupReceived(PopupReceiverIF.popupTypes.GROUP, popup);
            else receiver.popupReceived(PopupReceiverIF.popupTypes.PRIVATE_OTHER, popup);
            hm.put(hash, System.currentTimeMillis());
        } else if (hm.get(hash) + timeout < System.currentTimeMillis()) {
            hm.remove(hash);
            addFullPopup(srcName, srcIp, srcMac, srcNetbios, dstName, dstIp, dstMac, text);
        } else {
            hm.put(hash, System.currentTimeMillis());
        }

    }

    public synchronized void add(String srcName, String srcIp, String srcMac, String srcNetbios, String dstName, String dstIp, String dstMac, String text) {
        if (!(text.startsWith("~<--~") || text.endsWith("~-->~"))) {
            addFullPopup(srcName, srcIp, srcMac, srcNetbios, dstName, dstIp, dstMac, text);
        } else {
            String[] popup;
            if ((popup = NameDataTest.get(srcName)) != null) {                                                       //not new popup
                if (text.startsWith("~<--~") && !text.endsWith("~-->~")) {                                          //last part
                    addFullPopup(srcName, srcIp, srcMac, srcNetbios, dstName, dstIp, dstMac, popup[6] + text.substring(5)); //proceed if finished
                    NameDataTest.remove(srcName);                                                                    //delete from queue
                } else {
                    if (text.startsWith("~<--~")) text = text.substring(5);
                    if (text.endsWith("~-->~")) text = text.substring(0, text.length() - 5);
                    String newText = popup[6] + text;                                                               //new popup
                    NameDataTest.put(srcName, new String[]{srcIp, srcMac, srcNetbios, dstName, dstIp, dstMac, newText});    //add new part to old
                }
            } else {
                if (text.endsWith("~-->~"))
                    text = text.substring(0, text.length() - 5);                                                    //new popup
                NameDataTest.put(srcName, new String[]{srcIp, srcMac, srcNetbios, dstName, dstIp, dstMac, text});           //add new part to old
            }
        }
    }
}